package jiyun.com.yiyanmeng;


import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.LinearLayout;
import android.widget.TextView;


import base.BaseApp;
import base.BaseFragment;
import presenter.MyPresenter;
import utils.SpUtil;
import utils.ToastUtil;
import view.MyView;


/**
 * A simple {@link Fragment} subclass.
 */

public class MyFragment extends BaseFragment<MyView, MyPresenter> implements MyView,View.OnClickListener{
    private ImageView head_iv;
    private LinearLayout about;
    private LinearLayout kefu;


    public static MyFragment newInstance() {
        
        Bundle args = new Bundle();
        
        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void showToast(String msg) {

    }

    @Override
    protected MyPresenter bindPresenter() {
        return new MyPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

        head_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),MeHeadActivity.class));
            }
        });

        about.setOnClickListener(this);
        kefu.setOnClickListener(this);


    }

    @Override
    protected void initView(View view) {

        head_iv = view.findViewById(R.id.head_iv);

        about = view.findViewById(R.id.about);
        kefu = view.findViewById(R.id.kefu);
        TextView userName = view.findViewById(R.id.username_tv);
        String name = SpUtil.get(getActivity(), "u_name", "用户名");
        userName.setText(name);


    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_my;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.about:
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.kefu:
                if (isLogin()){
                    callPhone();
                }else {
                    ToastUtil.show(getActivity(),"请先登录！");
                }


                break;

        }

    }

    private void callPhone() {
        //1.拨打电话
// 给移动客服10086拨打电话
        try {
            Uri uri = Uri.parse("tel:10086");
            Intent intent = new Intent(Intent.ACTION_DIAL, uri);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public boolean isLogin() {
        boolean isLogin;
        String access_token = SpUtil.get(getActivity(), "access_token", "");
        if (!TextUtils.isEmpty(access_token)){
            isLogin=true;
        }else {
            isLogin=false;
        }
        return isLogin;
    }
}
